#!/bin/sh
hostfile=/root/host/foc2new.host

ALL_host=`more $hostfile  | awk '{print $2}' | awk '{{printf"%s, ",$0}}'| sed s/,//g`
echo "ALL HOST $ALL_host"
ip_ALL=( $ALL_host  )

HY_host=`more $hostfile| grep HY  | awk '{print $2}' | awk '{{printf"%s, ",$0}}'| sed s/,//g`
echo "HY HOST $HY_host"
ip_HY=( $HY_host  )

CU_host=`more $hostfile| grep CU  | awk '{print $2}' | awk '{{printf"%s, ",$0}}'| sed s/,//g`
echo "CU HOST $CU_host"
ip_CU=( $CU_host )

CT_host=`more $hostfile| grep CT  | awk '{print $2}' | awk '{{printf"%s, ",$0}}'| sed s/,//g`
echo "CT HOST $CT_host"
ip_CT=( $CT_host  )

canshu(){

        app_line=`grep $1 $hostfile | awk '{print $1}'`
        app_ipaddr=`echo $1 | awk -F _ '{print $1}'`
        app_user=`grep $1 $hostfile | awk '{print $3}'`
        app_port=`grep $1 $hostfile | awk '{print $4}'`
        app_run_path=`grep $1 $hostfile | awk '{print $5}'`
        app_content_path=`grep $1 $hostfile | awk '{print $6}'`
        app_today_log_path=`grep $1 $hostfile | awk '{print $7}'`
        app_history_log_path=`grep $1 $hostfile | awk '{print $8}'`

}

rsync_host(){

        host=`more $hostfile| grep $1  | awk '{print $2}' | awk '{{printf"%s, ",$0}}'| sed s/,//g`
        echo "$1 HOST $host"
        ip_host=( $host  )

        for ((k=0; k<=`expr "${#ip_host[*]}" - 1`;k++)) ;do

                canshu ${ip_host[k]}
     		rsync -avzP /u02/applications/foc2new/*  -e ssh  $app_ipaddr:$app_content_path
		echo "$app_ipaddr:$app_content_path"
		if [ -f /root/updaterecord/foc2new_${app_ipaddr}_${app_port}_rsync.log ];then mv /root/updaterecord/foc2new_${app_ipaddr}_${app_port}_rsync.log /root/updaterecord/foc2new_${app_ipaddr}_${app_port}_rsync.log_`date +%F_%H%M`;fi
		./rsync_check.sh /u02/applications/foc2new $app_ipaddr $app_content_path > /root/updaterecord/foc2new_${app_ipaddr}_${app_port}_rsync.log
		echo "$app_ipaddr $app_content_path"
                sleep 3
		sum_file=`more /root/updaterecord/${app_ipaddr}_${app_port}_rsync.log |grep "rsync\ file" | wc -l `
		sum_file_sucess=`more /root/updaterecord/${app_ipaddr}_${app_port}_rsync.log |grep "rsync\ file" | grep sucess| wc -l `
		sum_file_faild=`more /root/updaterecord/${app_ipaddr}_${app_port}_rsync.log |grep "rsync\ file" | grep faild| wc -l `

		sum_folder=`more /root/updaterecord/${app_ipaddr}_${app_port}_rsync.log |grep "rsync\ folder" | wc -l `
		sum_folder_sucess=`more /root/updaterecord/${app_ipaddr}_${app_port}_rsync.log |grep "rsync\ folder" | grep sucess| wc -l `
		sum_folder_faild=`more /root/updaterecord/${app_ipaddr}_${app_port}_rsync.log |grep "rsync\ folder" | grep faild| wc -l `

		echo -e "$app_ipaddr $app_content_path 总共同步文件$sum_file,成功$sum_file_sucess个，失败$sum_file_faild. \n总共同步文件夹$sum_folder,成功$sum_folder_sucess个，失败$sum_folder_faild个"
              
		
        done

}

hy() {
read -p "rsync Hang You Server,Are you sure[y/n]:" -t  10 hyc
case "$hyc"  in  
                y)
                echo "rsync hy"
                rsync_host HY
                ;;
                n)
                exit 0
                ;;
                *)
                echo "input error,please run it again!"
                ;;

esac

}

ct() {
echo "rsync ct"
rsync_host CT
}

cu() {
echo "rsync cu"
rsync_host CU
}

rsync() {
echo -e "which host foc2new app will you rsync \n 1):hangyou \n 2):CT \n 3):CU \n 4):ALL \n 5):quit"

read -p "Enter you choice:" -t  10 chiose

case "$chiose" in
                1)
                hy
                ;;
                2)
                ct
                ;;
                3)
                cu
                ;;
                4)
                hy
                ct
                cu
                ;;
                5)
                exit 0
                ;;
                *)
                echo "input error,please run it again!"
                ;;

esac

}



ls /mnt/100 > /dev/null
if [ $? -ne 0 ];then
umount -l /mnt/100
mount //192.168.190.100/d$ /mnt/100 -o username=backup,password=backup

if echo `mount` |grep -q "/mnt/100" ;then
	echo "/mnt/100 have mounted"
then
	mount //192.168.190.100/d$ /mnt/100 -o username=backup,password=backup
fi

chown weblogic10:bea /u02 -R

if [ `ls -lR /mnt/100/"syn for foc2new"/app/foc2new/ |wc -l` = `ls -lR /u02/applications/foc2new |wc -l` ]
then

	rsync

else

	echo "Update code differences, please re upload code !!!"

fi

cd /u02/applications/foc2new
rm -rf *
